<template>
  <div class="user-list">
    <div>
      <label>{{ $t("route.Personnel_management") }}</label>
      <div></div>
      <button>
        <img /><label>{{ $t("route.Employee_list") }}</label>
      </button>
    </div>

    <div>
      <div>
        <!-- <el-select
          @change="selectChange1"
          style="margin-left: 10px"
          v-model="query.factory"
          placeholder="请选择工厂"
        >
          <el-option v-for="item in factoryList" :key="item" :label="item" :value="item">
          </el-option>
        </el-select> -->
        <el-cascader :options="options" @change="proLineChange" />
        <el-select
          @change="selectChange1"
          style="margin-left: 10px"
          v-model="query.userType"
          :placeholder="$t('route.Please_select_a_job')"
        >
          <el-option
            v-for="item in userTypeList"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <div class="div-handle-right">
          <el-button @click="regCodeClick">{{
            $t("route.Registration_invitation_code")
          }}</el-button>
          <el-button @click="jumpPage(0)">{{ $t("route.Add") }}</el-button>
          <el-button>{{ $t("route.Delete") }}</el-button>
          <el-button @click="searchClick">{{ $t("route.search") }}</el-button>
          <input
            v-model="query.search"
            :placeholder="
              $t('route.number') +
              '/' +
              $t('route.full_name') +
              '/' +
              $t('route.cell_phone_number')
            "
          />
        </div>
      </div>

      <el-table class="table" :data="userList" style="width: 100%">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="username" :label="$t('route.number')" />
        <el-table-column prop="nickname" :label="$t('route.full_name')" />
        <el-table-column prop="mobile" :label="$t('route.cell_phone_number')" />
        <el-table-column :label="$t('route.post')">
          <template #default="scope">
            {{ formatUserType(scope.row.userType) }}
          </template>
        </el-table-column>
        <el-table-column prop="factory" label="工厂" />
        <el-table-column prop="workshop" :label="$t('route.Workshop')" />
        <el-table-column prop="proLine" :label="$t('route.Grou')" />
        <el-table-column prop="entry" :label="$t('route.Arrival_time')" />
        <el-table-column :label="$t('route.operate')">
          <template #default="scope">
            <el-button @click="jumpPage(1, scope.row)" type="text">{{
              $t("route.Editing")
            }}</el-button>
            <el-button
              @click="deleteClick(scope.row.id)"
              style="color: #ff0000"
              type="text"
              >{{ $t("route.Delete") }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          style="float: right"
          background
          @current-change="handlePageChange"
          :current-page="query.pageNum"
          :page-size="query.pageSize"
          layout="total, prev, pager, next"
          :total="query.total"
        ></el-pagination>
      </div>
    </div>

    <el-dialog
      width="400px"
      destroy-on-close
      :title="$t('route.Registration_invitation_code')"
      v-model="dgVis1"
    >
      <div class="qrcode">
        <div>{{ $t("route.General_worker_registration") }}</div>
        <div id="qrcode" ref="qrcode"></div>
        <div>{{ regCode }}</div>
        <el-button @click="downLoadClick">{{ $t("route.download") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import http from "../../js/http/http.js";
import api from "../../js/http/api.js";
import CommonUtils from "../../js/common/common-utils.js";
import QRCode from "qrcodejs2"; //二维码
import Constant from "../../js/common/constant.js";
import i18n from "../../i18n";
export default {
  data() {
    return {
      query: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
        search: null,
        userType: null,
        workshop: null,
        proLine: null,
      },
      userList: [],
      dgVis1: false,
      regCode: null,
      userTypeList: [
        {
          name: i18n.global.t("route.All_positions"),
          value: null,
        },
        ...Constant.USER_TYPE,
      ],
      workshopList: [],
      factoryList: [],

      options: null,
    };
  },
  methods: {
    getAllFactory() {
      http.post(
        api.GET_ALL_FACTORY,
        {
          adminId: this.$store.state.user.id,
        },
        (data) => {
          this.factoryList.push("全部工厂");
          for (var item of data.data) {
            this.factoryList.push(item.name);
          }
        }
      );
    },
    proLineChange(value) {
      this.query.workshop = value[0];
      this.query.proLine = value[1];
      console.log(this.query.workshop);
      console.log(this.query.proLine);
      this.query.search = null;
      this.query.pageNum = 1;
      this.getUserList();
    },
    getPorLineRow() {
      http.post(
        api.GET_POR_LINE_ROW,
        {
          adminId: this.$store.state.user.id,
        },
        (data) => {
          this.options = data.data;
          this.options.unshift({
            value: this.$t("route.All_workshops"),
            label: this.$t("route.All_workshops"),
          });
        }
      );
    },
    workshopChange() {
      this.query.pageNum = 1;
      this.query.search = null;
      this.getUserList();
    },
    selectChange1() {
      this.query.pageNum = 1;
      this.getUserList();
    },
    qrcode() {
      const qrcode = new QRCode(this.$refs.qrcode, {
        // 在属性ref="qrcode"的div里边添加二维码
        width: 200,
        height: 200, // 高度
        colorLight: "#ffffff",
        text: this.regCode, // 二维码内容（接口返回的数字或者什么）
      });
    },
    jumpPage(type, param1) {
      if (type == 0) {
        this.$router.push("add-user");
      } else if (type == 1) {
        this.$router.push({
          name: "add-user",
          params: {
            user: JSON.stringify(param1),
          },
        });
      }
    },

    regCodeClick() {
      this.dgVis1 = true;
      this.createRegCode();
    },

    deleteClick(userId) {
      this.$confirm(i18n.global.t("route.continue21"), i18n.global.t("route.reminder"), {
        confirmButtonText: i18n.global.t("route.sure"),
        cancelButtonText: i18n.global.t("route.cancel"),
        type: "warning",
      }).then(() => {
        http.post(
          api.DELETE_USER,
          {
            userId: userId,
          },
          (data) => {
            this.$message.success(i18n.global.t("route.Delete_succeeded"));
            this.getUserList();
          }
        );
      });
    },
    getUserList() {
      http.post(
        api.GET_USER_LIST,
        {
          adminId: this.$store.state.user.id,
          factory: Constant.FACTORY == "全部工厂" ? null : Constant.FACTORY,
          pageNum: this.query.pageNum,
          pageSize: this.query.pageSize,
          userType: this.query.userType,
          search: this.query.search,
          workshop:
            this.query.workshop == this.$t("route.All_workshops")
              ? null
              : this.query.workshop,
          proLine: this.query.proLine,
        },
        (data) => {
          this.userList = data.data.userList;
          this.query.total = data.data.total;
        }
      );
    },
    createRegCode() {
      http.post(
        api.CREATE_REG_CODE,
        {
          adminId: this.$store.state.user.id,
          userType: 15,
        },
        (data) => {
          this.regCode = data.data;
          this.qrcode();
        }
      );
    },
    formatUserType(userType) {
      return CommonUtils.formatUserType(userType);
    },
    handlePageChange(pageNum) {
      this.query.pageNum = pageNum;
      this.getUserList();
    },
    searchClick() {
      this.query.pageNum = 1;
      this.getUserList();
    },
    downLoadClick() {
      let myCanvas = document.getElementById("qrcode").getElementsByTagName("canvas");
      let a = document.createElement("a");
      a.href = myCanvas[0].toDataURL("image/png");
      a.download = i18n.global.t("route.QR_code");
      a.click();
      this.$message({
        message: i18n.global.t("route.Downloading_and_saving"),
        type: "success",
      });
    },
    getAllWorkshop() {
      http.post(
        api.GET_ALL_WORKSHOP_NAME,
        {
          adminId: this.$store.state.user.id,
          factory: Constant.FACTORY == "全部工厂" ? null : Constant.FACTORY,
        },
        (data) => {
          this.workshopList = [i18n.global.t("route.All_workshops"), ...data.data];
        }
      );
    },
  },
  created() {
    this.getPorLineRow();
    this.getUserList();
    this.getAllWorkshop();
    this.getAllFactory();
  },
};
</script>
<style lang="scss" scoped>
.user-list {
  color: #444444;
  font-size: 18px;
  padding-right: 20px;
  height: calc(100vh - 100px);
  // overflow: scroll;
  scrollbar-width: none;
  /* firefox */
  -ms-overflow-style: none;
  /* IE 10+ */
  overflow-x: hidden;
  overflow-y: auto;

  > div {
    display: flex;
    flex-direction: row;
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px;
    border-radius: 5px;
  }

  > div:nth-of-type(1) {
    > label:nth-of-type(1) {
      font-weight: 600;
    }

    > div:nth-of-type(1) {
      width: 1px;
      background-color: #cccccc;
      height: 15px;
      margin: 0px 20px;
    }

    > button:nth-of-type(1) {
      background-image: url(../../assets/img/customer-1.png);
      background-repeat: no-repeat;
      background-size: 100% 100%;
      padding: 5px 10px;
      border: 0px;
      display: flex;
      color: #ffffff;
      flex-direction: row;
      align-items: center;
      font-weight: 800;

      > img {
        margin-right: 7px;
        width: 15px;
        height: 15px;
        content: url(../../assets/img/customer-2.png);
      }
    }
  }

  > div:nth-of-type(2) {
    flex-direction: column;

    > div:nth-of-type(1) {
      display: flex;
      flex-direction: row;
      align-items: center;

      .div-handle-right {
        flex: 1;
        flex-direction: row-reverse;
        text-align: right;
        display: flex;
        align-items: center;

        > input:nth-of-type(1) {
          border: 1px solid #eeeeee;
          font-size: 15px;
          width: 200px;
          padding: 5px 8px;
          outline: none;
        }

        > button {
          color: #a866ff;
          margin-left: 10px;
          padding: 4px 20px;
        }
      }
    }

    .table {
      margin-top: 20px;
    }
  }

  .qrcode {
    padding: 0px 80px;

    > div:nth-of-type(1),
    > div:nth-of-type(3) {
      font-weight: 800;
      font-size: 25px;
      margin: 10px 0px;
      text-align: center;
    }

    > div:nth-of-type(1) {
      font-size: 20px;
    }

    > button {
      width: 100%;
      margin-top: 10px;
    }
  }
}
</style>
